/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#pragma once

#include <kmhdfs/client/user_info.h>
#include <kmhdfs/common/hash.h>

#include <string>

namespace Hdfs {
namespace Internal {

enum AuthMethod {
    SIMPLE = 80, KERBEROS = 81, //"GSSAPI"
    TOKEN = 82, //"DIGEST-MD5"
    UNKNOWN = 255
};

enum AuthProtocol {
    NONE = 0, SASL = -33
};

class RpcAuth {
public:
    RpcAuth() :
        method(SIMPLE) {
    }

    explicit RpcAuth(AuthMethod mech) :
        method(mech) {
    }

    RpcAuth(const UserInfo & ui, AuthMethod mech) :
        method(mech), user(ui) {
    }

    AuthProtocol getProtocol() const {
        return method == SIMPLE ? AuthProtocol::NONE : AuthProtocol::SASL;
    }

    const UserInfo & getUser() const {
        return user;
    }

    UserInfo & getUser() {
        return user;
    }

    void setUser(const UserInfo & user) {
        this->user = user;
    }

    AuthMethod getMethod() const {
        return method;
    }

    void setMethod(AuthMethod method) {
        this->method = method;
    }

    size_t hash_value() const;

    bool operator ==(const RpcAuth & other) const {
        return method == other.method && user == other.user;
    }

public:
    static AuthMethod ParseMethod(const std::string & str);

private:
    AuthMethod method;
    UserInfo user;
};

}
}

HDFS_HASH_DEFINE(::Hdfs::Internal::RpcAuth);
